package net.expectx.micro.shop.controller.memberUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseResult;
import net.expectx.common.single.util.Md5Util;
import net.expectx.micro.shop.common.AdminConstants;
import net.expectx.micro.shop.common.AdminResult;
import net.expectx.micro.shop.common.util.ExpectXUtil;
import net.expectx.micro.shop.controller.ManagerController;
import net.expectx.micro.shop.dao.bean.*;
import net.expectx.micro.shop.rpc.api.MemberUserReceivingAddressService;
import net.expectx.micro.shop.rpc.api.MemberUserService;
import net.expectx.micro.shop.rpc.api.PermissionUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author hxs
 */

@Controller
@RequestMapping("/manager/member/user/")
@Api(value = "会员管理", description = "会员管理")
public class MemberUserController {
    private static final Logger LOGGER = Logger.getLogger(ManagerController.class);

    @Autowired
    MemberUserService memberUserService;
    @Autowired
    MemberUserReceivingAddressService memberUserReceivingAddressService;

    @ApiOperation(value = "会员首页")
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        return "memberUser/index";
    }

    @ApiOperation(value = "新增会员")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "memberUser/modify";
    }

    @ApiOperation(value = "修改会员")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        MemberUser memberUser = memberUserService.selectByPrimaryKey(id);
        modelMap.put("memberUser", memberUser);
        return "memberUser/modify";
    }
    @ApiOperation(value = "会员详情")
    @RequestMapping(value = "/details/{id}", method = RequestMethod.GET)
    public String details(@PathVariable("id") Long id, ModelMap modelMap) {
        MemberUser memberUser = memberUserService.selectByPrimaryKey(id);
        modelMap.put("memberUser", memberUser);
        return "memberUser/memberInfo";
    }
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object page(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request,String userName) {
        Map<String, Object> result = new HashMap<>(16);
        MemberUserExample memberUserExample = new MemberUserExample();
        /**
         * 创造条件
         */
        MemberUserExample.Criteria criteria = memberUserExample.createCriteria();

        if (StringUtils.isNotBlank(userName)){
            criteria.andUserNameEqualTo(userName);
        }
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        /**
         * 分页
         */
        memberUserExample.setPageIndex(page);
        memberUserExample.setPageSize(limit);
        /**
         * 排序
         */
        memberUserExample.setOrderByClause("gmt_modified desc");

        List<MemberUser> memberUsers = memberUserService.selectByExample(memberUserExample);

        long total = memberUserService.countByExample(memberUserExample);

        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", memberUsers);
        return result;
    }

    @ApiOperation(value = "新增会员")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(MemberUser memberUser) {
        memberUser.setGmtCreate(System.currentTimeMillis());
        memberUser.setGmtModified(System.currentTimeMillis());
        memberUser.setIsDeleted((byte) 0);
        memberUser.setIsLocked(AdminConstants.NO);
        memberUser.setLastLoginTime(System.currentTimeMillis());
        memberUser.setSalt(UUID.randomUUID().toString().toLowerCase().replace("-",""));
        memberUser.setPassword(Md5Util.md5(memberUser.getPassword()+memberUser.getSalt()));
        return memberUserService.insertSelective(memberUser);
    }

    @ApiOperation(value = "修改会员")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, MemberUser memberUser) {
        memberUser.setGmtModified(System.currentTimeMillis());
        return  memberUserService.updateByPrimaryKeySelective(memberUser);
    }

    @ApiOperation(value = "删除会员")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object physicalRemove(@PathVariable("ids") String ids) {
        MemberUserExample memberUserExample = new MemberUserExample();
        memberUserExample.createCriteria().andIdIn(ExpectXUtil.toList(ids,","));
        return memberUserService.physicalRemoveByExample(AdminConstants.YES, memberUserExample);
    }
    @ApiOperation(value = "会员收货地址列表")
    @RequestMapping(value = "/addressList/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object addressList(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @PathVariable("id") Long id) {
        MemberUserReceivingAddressExample memberUserReceivingAddressExample = new MemberUserReceivingAddressExample();
        MemberUserReceivingAddressExample.Criteria criteria = memberUserReceivingAddressExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        criteria.andUserIdEqualTo(id);

        memberUserReceivingAddressExample.setPageIndex(page);
        memberUserReceivingAddressExample.setPageSize(limit);

        List<MemberUserReceivingAddress> memberUserAll = memberUserReceivingAddressService.selectByExample(memberUserReceivingAddressExample);
        long total = memberUserReceivingAddressService.countByExample(memberUserReceivingAddressExample);
        Map<String, Object> result = new HashMap<>(16);
        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", memberUserAll);
        return result;
    }

}
